package com.ddch.tinkerlib.utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * created by 韦敏敏
 * on 2019/12/9
 *  sourceFile  --  源文件
 *  targetFile  --   目标文件
 *  IOException --   IO异常
 */

public class FileUtils {
    public static void copyFile(File sourceFile, File targetFile) throws IOException {
        // 新建文件输入流，对它进行缓冲
        FileInputStream input = new FileInputStream(sourceFile);
        BufferedInputStream inBuff = new BufferedInputStream(input);
        // 新建文件输出流，并对它进行缓冲
        FileOutputStream output = new FileOutputStream(targetFile);
        BufferedOutputStream outBuffer = new BufferedOutputStream(output);
        // 缓冲数组
        byte[] b = new byte[1024 * 5];
        int len;
        while ((len = inBuff.read(b)) != -1){
            outBuffer.write(b, 0, len);
        }
        // 刷新此缓冲的输出流
        outBuffer.flush();
        // 关闭流
        inBuff.close();
        outBuffer.close();
        output.close();
        input.close();
    }
}
